package com.pub.tools;

import java.util.*;

/**
 * 目的：用以存取Property File資料
 * 用於：目前有QueryBean.java
 */
public class PropertyBean {
  private ResourceBundle rb = null;
  private boolean gblnRealTimeReloadable=false;

  public static class ResClassLoader extends ClassLoader {
          ResClassLoader(ClassLoader parent) {
                  super(parent);
          }
  }

  public PropertyBean(String ptyType,boolean blnRealTimeReloadable){
      gblnRealTimeReloadable=blnRealTimeReloadable;
      init(ptyType);
  }

  /*private static PropertyBean getProperty(String ptyType,boolean blnRealTimeReloadable){
      return new PropertyBean(ptyType,blnRealTimeReloadable);
  }*/


  public PropertyBean(String ptyType){
      init(ptyType);
  }

  public PropertyBean(ResourceBundle prblProperties){
      rb=prblProperties;
  }

  private void init(String ptyType) {
      if(gblnRealTimeReloadable){//可即時reload property 不用重開 ap server
          rb = ResourceBundle.getBundle(ptyType, Locale.getDefault(),
                                        new
                                        ResClassLoader(getClass().getClassLoader())); //代表要讀取的文字檔，例: DB_Config
      }
      else{
          rb = ResourceBundle.getBundle(ptyType, Locale.getDefault()); //代表要讀取的文字檔，例: DB_Config
      }
  }

  public String getString(String ptyStr) { return rb.getString(ptyStr); }
  /*public String getString(String aKey,String[] args){
    return MessageFormat.format(getString(aKey),args);
  }*/
  public int getInt(String ptyStr)       { return Integer.decode(rb.getString(ptyStr)).intValue();  }
  public boolean getBoolean(String ptyStr) { return new Boolean(rb.getString(ptyStr)).booleanValue(); }
}
